//
//  NSString+GX_MD5.m
//  24-加密
//
//  Created by 曹曹 on 16/8/16.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "NSString+GX_MD5.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (GX_MD5)

- (NSString *)MD5String32BitsLowercase {
    return [self MD5String32Bits].lowercaseString;
}
- (NSString *)MD5String32BitsUppercase {
    
    return [self MD5String32Bits].uppercaseString;
}
- (NSString *)MD5String32Bits {
    // 1.将字符串转成成C语言字符串
    const char *cString = [self UTF8String];
    // 长度
    CC_LONG length = (CC_LONG)strlen(cString);
    // 容器(字符数组)
    unsigned char bytes[CC_MD2_DIGEST_LENGTH];
    // 2.将C的字符串进行MD5处理并放入容器
    CC_MD5(cString, length, bytes);
    // 3.将容器中的内容转拼接成一个OC的字符串并返回
    NSMutableString *finalMD5String = [NSMutableString stringWithCapacity:CC_MD2_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD2_DIGEST_LENGTH; i++) {
        [finalMD5String appendFormat:@"%02x", bytes[i]];
    }
    return finalMD5String;
}
- (NSString *)MD5String16BitsLowercase {
    return [self MD5String16Bits].lowercaseString;
}
- (NSString *)MD5String16BitsUppercase {
    return [self MD5String16Bits].uppercaseString;
}
- (NSString *)MD5String16Bits {
    return [[self MD5String32Bits] substringWithRange:NSMakeRange(8, 16)];
}

@end
